<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>2仿京东显示密码</title>

    <style>
        .box {
            position: relative;
            width: 400px;
            border-bottom: 1px solid rebeccapurple;
            margin: 100px auto;
        }

        .box input {
            width: 370px;
            height: 30px;
            border: 0;
            /* 去掉表单元素的边框 */
            outline: none;
        }

        .box img {
            position: absolute;
            top: 2px;
            right: -1px;
            width: 24px;

        }
    </style>
</head>

<body>
    <div class="box">
        <label for="">
            <img src="./img/close.png" alt="">
        </label>
        <input type="password" name="" id="pwd">
    </div>

    <script>
        var ipt = document.getElementById("pwd");
        var img = document.querySelector("img");
        var i = 0;
        img.onclick = function () {
            i++;
            i = i % 2;
            if (i == 1) {
                img.src = "./img/open.png";
                pwd.type = 'text';
                
            }
            if (i == 0) {
                img.src = "./img/close.png";
                pwd.type = 'password';
                
            }

        }


    </script>

</body>

</html>